package com.jxwifi.cloud.quickclean.quicktreasure;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.adapter.SetMealAdapter;
import com.jxwifi.cloud.quickclean.app.LocationGaoDe;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.PayParamsBean;
import com.jxwifi.cloud.quickclean.bean.ShopRechargeBean;
import com.jxwifi.cloud.quickclean.bean.SystemBean;
import com.jxwifi.cloud.quickclean.bean.UcCustomerBank;
import com.jxwifi.cloud.quickclean.main.WebViewsActivity;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.pay.PayUtil;
import com.jxwifi.cloud.quickclean.pay.WxpayParams;
import com.jxwifi.cloud.quickclean.popwindow.PopPayDepositModule;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.popwindow.PopWindowUtil;
import com.popwindow.Popwindows;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.ArrayList;
import java.util.List;

public class BuySetMealPresenter {

    private Context mContext;
    private BuySetMealView mView;
    private AuzImpl mAuz;

    private SetMealAdapter mAdapter;

    private String rechargeCode;//套餐购买编码
    private String mCustomer;
    private double mDepositl;

    private String mStatus;//余额状态

    public BuySetMealPresenter(Context context, BuySetMealView view) {
        this.mContext = context;
        this.mView = view;
        mAuz = new AuzImpl();
    }

    public void initView() {
        mView.getRvList().setLayoutManager(new LinearLayoutManager(mContext));
        mView.getRvList().setAdapter(mAdapter = new SetMealAdapter(R.layout.list_set_meal_item));
        mAdapter.openLoadAnimation();
        mAdapter.openLoadAnimation(BaseQuickAdapter.FOOTER_VIEW);
        initData();
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < mAdapter.getData().size(); i++) {
                    mAdapter.getData().get(i).setChechCK(false);
                }
                mAdapter.getData().get(position).setChechCK(true);
                rechargeCode = mAdapter.getData().get(position).getRechargeCode();
                mDepositl=Double.valueOf(mAdapter.getData().get(position).getRechargeAmount());
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    public void initData() {
        Params params = new Params();
        Log.w("oushuhua", "province" + LocationGaoDe.getProvince() + "city" + LocationGaoDe.getCity() + "area" + LocationGaoDe.getArea());
        params.add("area", LocationGaoDe.getArea());//area
        params.add("city", LocationGaoDe.getCity());//city
        params.add("province", LocationGaoDe.getProvince());//province
        mAuz.auzpost(UrlPool.POST_RECHARGE_LOOKUP, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                //ShopRechargeBean
                if (code==0){
                    List<ShopRechargeBean> mListBean = JSON.parseArray(data, ShopRechargeBean.class);
                    if (mListBean.size() > 0) {
                        mListBean.get(0).setChechCK(true);
                        rechargeCode = mListBean.get(0).getRechargeCode();
                        mDepositl=Double.valueOf(mListBean.get(0).getRechargeAmount());

                        mAdapter.setNewData(mListBean);
                    }else{
                        mAdapter.setEmptyView(mView.getViews());
                    }
                }else{
                    mView.oninitData();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

    public void paySetMeal(String starts) {
        if (mAdapter.getData().size()>0){
            if(StateUtils.NORMAL.equals(starts)){
                PopPayDepositModule mPopPayOrder = new PopPayDepositModule(mContext, mDepositl, "支付金额", new PopPayDepositModule.PopPayListener() {
                    @Override
                    public void getPayOrder(String PayType, String PayPrice) {
                        onprepay(PayType);
                    }
                });
                Popwindows window = PopWindowUtil.getPopupWindow(mPopPayOrder.getView(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
                        .LayoutParams.MATCH_PARENT);
                mPopPayOrder.setPopwindow(window);
                window.show(mView.getRvList(), Popwindows.Location.TOP);
            }else{
                Toast toast = Toast.makeText(mContext, "账户处于冻结状态，暂时无法购买，请联系我们", Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
            }
        }
    }

    public void onprepay(final String PayType) {
        Params params = new Params();
        params.add("payChannelEnum", PayType);
        params.add("rechargeCode", rechargeCode);
        params.add("tradeType", "APP");
        mAuz.auzpost(UrlPool.POST_RECHARGE_PUR_RECHARGE, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code==0){
                    PayParamsBean mParams = JSON.parseObject(data, PayParamsBean.class);
                    if (StateUtils.PAY_ALI.equals(mParams.getPayChannelEnum())) { //支付宝
                        PayUtil.getInstance().aliPay(mContext, mParams);
                    } else if (StateUtils.PAY_WX.equals(mParams.getPayChannelEnum())) { //微信
                        mView.getWXPayCode(mParams.getOutTradNo());
                        // mWXPay(mParams.getWxPayResult());
                        PayUtil.getInstance().WXPay(mContext, mParams.getWxPayResult());
                    }
                }else{
                    mView.ononprepay(PayType);
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

    //获取用户余额
    public void findbank(){
        mAuz.auzpost(UrlPool.POST_BANK_FINDBANK, new Params(), new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                UcCustomerBank mBean = JSON.parseObject(data, UcCustomerBank.class);
                if (mBean!=null){
                    paySetMeal(mBean.getStatus());
                }else{
                    paySetMeal(StateUtils.NORMAL);
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

    //购买说明
    public void BuyExplain(){
        Params params = new Params();
        mAuz.auzget(UrlPool.SYSTEM_LIST, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code == 0) {
                    List<SystemBean> msystemBean = JSON.parseArray(data, SystemBean.class);
                    for (int i = 0; i < msystemBean.size(); i++) {
                        if ("kjb_setmeal_describe_h5_url".equals(msystemBean.get(i).getKeyName())) {
                            mCustomer = msystemBean.get(i).getKeyValue();
                            break;
                        }
                    }
                    if (!TextUtils.isEmpty(mCustomer)) {
                        Intent intent=new Intent();
                        intent.setClass(mContext,WebViewsActivity.class);
                        intent.putExtra("web_view_url",mCustomer);
                        intent.putExtra("web_view_title","购买说明");
                        mContext.startActivity(intent);
                    }
                } else {
                    mView.onBuyExplain();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }



}
